package javaBase.nowcoder.HJ18_S_识别有效的IP地址和掩码并进行分类统计;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str;
        int AType = 0, BType = 0, CType = 0, DType = 0, EType = 0, personal = 0, error = 0;
        String regx = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\." +
                       "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\." +
                       "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\." +
                       "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";
        Pattern compile = Pattern.compile(regx);
        while ((str = br.readLine()) != null) {

            String ips[] = str.split("~", 2);
            String ip1 = ips[0], ip2 = ips[1];
            if (compile.matcher(ip1).matches() && compile.matcher(ip2).matches()) {
                Integer first1 = Integer.parseInt(ip1.split("\\.")[0]);
                Integer second = Integer.parseInt(ip1.split("\\.")[1]);
                if (!str.startsWith("0.") && !str.startsWith("127.")) {
                    if (isMask(ip2)) {
                        if (1 <= first1 && first1 <= 126) {
                            //System.out.println("A：" + str);
                            AType++;
                        } else if (128 <= first1 && first1 <= 191) {
                            //System.out.println("B：" + str);
                            BType++;
                        } else if (192 <= first1 && first1 <= 223) {
                            //System.out.println("C：" + str);
                            CType++;
                        } else if (224 <= first1 && first1 <= 239) {
                            //System.out.println("D：" + str);
                            DType++;
                        } else if (240 <= first1 && first1 <= 255) {
                            //System.out.println("E：" + str);
                            EType++;
                        }
                        if (10 == first1) {
                            //System.out.println("personal：" + str);
                            personal++;
                        }
                        if (172 == first1 && 16 <= second && second <= 31) {
                            //System.out.println("personal：" + str);
                            personal++;
                        }
                        if (192 == first1 && second == 168) {
                            //System.out.println("personal：" + str);
                            personal++;
                        }
                    } else {
                        //System.out.println("error：" + str);
                        error++;//错误掩码
                    }
                }
            } else {
                //System.out.println("error：" + str);
                error++;//错误IP地址
            }

        }
        br.close();
        System.out.println(AType + " " + BType + " " + CType + " " + DType + " " + EType + " " + error + " " + personal);
    }

    //6.72.161.12~255.252.0.0
    public static boolean isMask(String ip) {
        if ("255.255.255.255".equals(ip) || "0.0.0.0".equals(ip)) {
            return false;
        }
        String bi = "";
        int maskArr[] = Arrays.stream(ip.split("\\.")).mapToInt(Integer::parseInt).toArray();
        for (int i = 0; i < 4; i++) {
            String b = "00000000" + Integer.toBinaryString(maskArr[i]);
            b = b.substring(b.length() - 8, b.length());
            bi += b;
        }
        if (bi.substring(bi.indexOf('1'), bi.lastIndexOf("1")).contains("0")) {
            return false;
        } else {
            return true;
        }
    }
}
